package recharge_management

import (
	"context"
	"strconv"

	xerrors "gitee.com/liuxuezhan/ar-platform/admin-api/pkg/xerrors/assets"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeSmsPageListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 短信充值分页查询
func NewRechargeSmsPageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeSmsPageListLogic {
	return &RechargeSmsPageListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RechargeSmsPageListLogic) RechargeSmsPageList(req *types.RechargeSmsPageRequest) (resp *types.RechargeSmsPageResponse, err error) {
	request := rpc.RechargeSmsPageRequest{}
	err = copier.Copy(&request, req)

	if req.Param.MatchStatus == 0 {
		request.Param.MatchStatus = ""
	} else {
		request.Param.MatchStatus = strconv.FormatInt(int64(req.Param.MatchStatus), 10)
	}

	if err != nil {
		return nil, xerrors.ErrDataConvert
	}
	page, err := l.svcCtx.AssetsRpc.PayRecharge.RechargeSmsListPage(l.ctx, &request)
	response := types.RechargeSmsPageResponse{}
	if err != nil {
		return &response, err
	}
	err = copier.Copy(&response, page)
	for i := 0; i < len(response.DataList); i++ {
		key, err := strconv.ParseInt(page.DataList[i].MatchStatus, 10, 64)
		if err != nil {
			return &response, xerrors.ErrParse
		}
		response.DataList[i].MatchStatus = int32(key)
	}
	if err != nil {
		return nil, xerrors.ErrDataConvert
	}
	return &response, nil
}
